package com.gdit.zax.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.gdit.zax.dao.SearchDao;
import com.gdit.zax.entitys.ApplyTcInfo;
import com.gdit.zax.entitys.Course;
import com.gdit.zax.entitys.SearchM;
import com.gdit.zax.utils.DBUtil;

public class SearchDaoImpl implements SearchDao {
	private PreparedStatement ps;
	private ResultSet rs;
	
	private Connection conn;
	private HttpServletRequest request;
	private HttpServletResponse response;
	
	
	public SearchDaoImpl(Connection conn,HttpServletRequest request, HttpServletResponse response) {
		this.conn = conn;
		this.request = request;
		this.response = response;
	}
	
	public ArrayList<SearchM> listHotSearchUser(int page, int onepagecount) {
		return null;
	}

	public ArrayList<SearchM> listHotSearchVideo(int page, int onepagecount) {
		String sql = "select *,count(Id) as Count from t_search group by Content order by Count desc limit ?,?";
		ArrayList<SearchM> list = new ArrayList<SearchM>();
		try {
			ps = conn.prepareStatement(sql);
			ps.setInt(1, (page-1)*onepagecount);
			ps.setInt(2, onepagecount);
			rs = ps.executeQuery();
			while (rs.next()) {
				SearchM info = DBUtil.getobject(rs, SearchM.class);				
				list.add(info);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}

	public boolean addSearchRecod(int uid, int type, String key) {
		String sql = "insert t_search set UserId=?,Type=?,Content=?";
		try {
			ps = conn.prepareStatement(sql);
			ps.setInt(1, uid);
			ps.setInt(2, type);
			ps.setString(3, key);
			
			int resu = ps.executeUpdate();
			if (resu>0) {
				return true;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

	public int getUserSearchCount(String key) {
		// TODO Auto-generated method stub
		return 0;
	}

	public int getVideoSearchCount(String key) {
		// TODO Auto-generated method stub
		return 0;
	}

	public ArrayList<Course> sreachCrouse(String key, int page, int onepagecount) {
		ArrayList<Course> list = new ArrayList<Course>();
		String sql = "select * from t_course where Name like ?";
		
		try {
			ps = conn.prepareStatement(sql);
			ps.setString(1, "%" + key + "%");
			
			rs = ps.executeQuery();
			
			while(rs.next()){
				Course be = DBUtil.getobject(rs, Course.class);
				list.add(be);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}


}
